package com.zr.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.InsertNewDisstationDao;
import com.zr.model.Cdisstation;
import com.zr.model.Clog;
import com.zr.model.Cstationtype;
import com.zr.model.Cuser;

@Repository("insertNewDisstationDao")
public class InsertNewDisstationDaoImpl<T> extends BaseDaoImpl<T> implements InsertNewDisstationDao<T>{

	@Override
	public int getStataionExistsOrNot(String disname) {
		int i = 0;
		List<Cdisstation> ds = new ArrayList<Cdisstation>();
		String hql = "from Cdisstation where disname = :disname";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("disname", disname);
		ds = que.list();
		if (ds.size()>0) {
			i=1;
		} else {
            i=0;
		}
		return i;
	}

	@Override
	public void insertInfoIntoLog(String time, String cuname, int cuid, String info) {
		Clog clog = new Clog();
		clog.setLinfo(info);
		clog.setLtime(time);
		clog.setUname(cuname);
		clog.setUid(cuid);
		this.getCurrentSession().save(clog);
		
	}

	@Override
	public Cstationtype getDistypeByInfo(String distype) {
		List<Cstationtype> ds = new ArrayList<Cstationtype>();
		String hql = "from Cstationtype";
		Query que = this.getCurrentSession().createQuery(hql);
		ds = que.list();
		return ds.get(0);
	}

	@Override
	public void InsertNewDisstation(String disname, Cstationtype cstationtype, Cuser cuser, String disscope,
			double disprice, int instation) {
		Cdisstation cd = new Cdisstation();
		cd.setDisname(disname);
		cd.setCuser(cuser);
		cd.setCstationtype(cstationtype);
		cd.setDisprice(disprice);
		cd.setDisscope(disscope);
		cd.setInstation(instation);
		this.getCurrentSession().save(cd);
		
	}

	@Override
	public Cuser getUserByUid(int uid) {
		String hql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("uid", uid);
		Cuser u = (Cuser)que.uniqueResult();
		return u;
	}

	@Override
	public int getNewStationIdByDisname(String disname) {
		int i = 0;
		List<Cdisstation> ds = new ArrayList<Cdisstation>();
		String hql = "from Cdisstation where disname = :disname";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("disname", disname);
		ds = que.list();
		Cdisstation d = (Cdisstation)ds.get(0);
		i = d.getDisid();
		return i;
	}

	@Override
	public void updateUserByUidAndDisid(int uid, int disid) {
		String hql = "from Cuser where uid = :uid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("uid", uid);
		Cuser u = (Cuser)que.uniqueResult();
		u.setDisid(disid);
		this.getCurrentSession().save(u);
		
	}
	
	
	
}
